<?php

namespace apiend\modules\user\models;

use Yii;
use yii\data\ActiveDataProvider;

/**
 * 用户收藏的动态列表
 *
 * @author emhome <emhome@163.com>
 * @since 1.0
 */
class FavoriteDynamicSearch extends \common\models\dynamic\Dynamic {

    public $queryCacheDuration = false;
    public $uid;

    /**
     * Creates data provider instance with search query applied
     *
     * @param int $uid
     * @return ActiveDataProvider
     */
    public function search() {
        $query = static::findActive('t', static::STATUS_ACTIVE)
        ->with(['group', 'albums', 'video', 'stats', 'collects']);
        $query->innerJoinWith('collects c')->andWhere(['c.user_id' => $this->uid]);
        $query->orderBy([
            'c.created_at' => SORT_DESC,
        ]);
        $dataProvider = new ActiveDataProvider([
            'query' => $query,
        ]);
        return $dataProvider;
    }

    /**
     * @inheritdoc
     */
    public function fields() {
        return [
            'id',
            'user' => 'userText',
            'group',
            'subject',
            'format' => 'formatText',
            'albums',
            'video',
        ];
    }

}
